# -*- coding: utf-8 -*-

###########################################################################
#
#   Copyright (c) 2020  Bresee, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###########################################################################

"""
postgrs sql  main connection

create database table

"""

import utils.common.log as log
import utils.pgsql.base.pgsqlbase as pgsqlbase

import utils.pgsql.pguser
import utils.pgsql.pgtemplate
import utils.pgsql.pgadmin
import utils.pgsql.pgcase
#import utils.pgsql.pgcvm
import utils.pgsql.pgoperator
import utils.pgsql.pgdevice
import utils.pgsql.pgactivity
import utils.pgsql.pgvlogrecord
import utils.pgsql.pgpoint
import utils.pgsql.pgseller
import utils.pgsql.pgpay
import utils.pgsql.pgtechpay
import utils.pgsql.pgchannel
import utils.pgsql.pgtechpayflow
#import utils.pgsql.pgannualfee
import utils.pgsql.pgpaycount
import utils.pgsql.pgposition


pgMainConn = pgsqlbase.PGSqlBase()  #
boguan_db = pgsqlbase.PGSqlBase()

def boguanConnect(pgDic: dict):
    connect = None
    if boguan_db.connect(pgDic["host"], pgDic["port"], pgDic["user"], pgDic["pwd"], "vlog_data"):
        return boguan_db.conn
    return connect


def pgInit(pgDic: dict) -> bool:

    if (
        pgMainConn.connect(
            pgDic["host"], pgDic["port"], pgDic["user"], pgDic["pwd"], pgDic["dbname"]
        )
        is False
    ):
        log.logger.error("!!!!!  !!!!!!   postgrsql connect failed !!!!! !!!!!")
        return False
    else:
        log.logger.info(f"postgresql connect {pgDic['dbname']} success")

     
    # utils.pgsql.pguser.PgUser().createTbl()
    utils.pgsql.pgtemplate.PgTemplate().createTbl()
    utils.pgsql.pgadmin.PgAdmin().createTbl()
    utils.pgsql.pgcase.PgCase().createTbl()
    #utils.pgsql.pgcvm.PgCvm().createTbl()
    utils.pgsql.pgoperator.PgOperator().createTbl()
    utils.pgsql.pgdevice.PgDevice().createTbl()
    utils.pgsql.pgactivity.PgActivity().createTbl()
    utils.pgsql.pguser.PgUser().createTbl()
    utils.pgsql.pgvlogrecord.PgRecord().createTbl()
    utils.pgsql.pgpoint.PgPoint().createTbl()
    utils.pgsql.pgseller.PgSeller().createTbl()
    utils.pgsql.pgpay.PgPay().createTbl()
    utils.pgsql.pgtechpay.Pgtechpay().createTbl()
    utils.pgsql.pgchannel.PgChannel().createTbl()
    #utils.pgsql.pgpayflow.Pgpayflow().createTbl()
    utils.pgsql.pgtechpayflow.Pgtechpayflow().createTbl()
    #utils.pgsql.pgannualfee.PgAnnualfee().createTbl()
    utils.pgsql.pgpaycount.PgcountPay().createTbl()
    utils.pgsql.pgposition.PgPosition().createTbl()
    return True
